{{if false}}<script>{{/if}}
{{$controller = $smarty.const.CCTRL}}
{{$token = $smarty.const.TOKEN}}
{{include file="../00_base/function.tpl"}}
function handleEvents(){
    try{
        acolumns['CompanyName'].createeditor=function (row, cellvalue, editor) {
            editor.jqxDropDownList({ source: {{$acompanys|@json_encode}} });
        };
        acolumns['UnitPrice'].createeditor=function (row, cellvalue, editor) {
            editor.jqxNumberInput({ decimalDigits: 2 });
        };
        acolumns['UnitPrice'].validation= function (cell, value) {
            if (value < 0 || value > 1000000) {
                return { result: false, message: "Unit Price should be in the 0-1,000,000 interval" };
            }
            return true;
        };
        acolumns['Title'].validation= function (cell, value) {
            if (value =="") {
                return { result: false, message: "Title should be non-empty." };
            }
            return true;
        };
        myGrid.bind('cellendedit', function (event) {
            try{
                var args = event.args;
                var _column = args.datafield ,_row = args.rowindex,_value = args.value;
                var _data=myGrid.jqxGrid('getrowdata', _row);
                var _id=_data.ID;
                if(_id==undefined || _id==""){
                    return;
                }
                switch(_column){
                    case "Status":
                        if(_value=="true" || _value ==true) _value="Public";
                        else _value="Private";
                        updateCol(_column,_id,_value);
                        break;
                    case "CompanyName":
                        var _re={{$reacompanys|@json_encode}};
                        if(_value!=_data.CompanyName)updateCol("Type",_id,_re[_value]);
                        break;
                    case "Title":
                        var _re={{$reacompanys|@json_encode}};
                        if(_value!=_data.Title)updateCol(_column,_id,_value);
                        break;
                    case "UnitPrice":
                        if(_value!=_data.UnitPrice)updateCol(_column,_id,_value);
                        break;
                    default:
                    uiMessage("Unkown this cell. Maybe 'editable' of this colums is 'off'."); 
                }
            }catch(e){
                uiMessage(e.message);
                console.log("Error:"+e.message);
            }
        });
    }catch(e){tipMessage(e.message);}
}
function SaveItem(){
    try{
        if(isrunning)return;
        console.log("Save ↵ Call");
        var url="{{base_url()}}backend/{{$controller}}/save";
        var data={
            _id             :   $("#txtID").val(),
            _title          :   $("#txtTitle").val(),
            _alias          :   $("#txtAlias").val(),
            _unitprice      :   $("#txtUnitPrice").val(),
            _content        :   tinyMCE.get('txtContent').getContent(),
            _company        :   $("#cbxCompany").val(),
            _image      :   $("#txtThumb").val()
        }
        //ShowNoticeDialogMessage($("<div />").text(Content).html());return;
        console.log(data);
        isrunning=true;
        myAjax(url,data,function(data){
            isrunning=false;
            if(data.result>=0){
                uiMessage(data.message)
                myjqxGrid.CancelEdit();
                myjqxGrid.Refresh();
            }else{
                ShowErrorDialogMessage(data.message);
            }
        });
    }catch(e){
        console.log("JSON Error:"+e.message+"\n↵ Error");
        ShowErrorDialogMessage(e.message);
        //ShowErrorDialogMessage("Sorry. Your request could not be completed.<br/> Please check your input data.");
    }
}
